package cn.edu.sgu.www.mhxysy.service.potency.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.potency.PotencyFruitPatternDTO;
import cn.edu.sgu.www.mhxysy.entity.potency.PotencyFruitPattern;
import cn.edu.sgu.www.mhxysy.mapper.potency.PotencyFruitPatternMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.pager.potency.RolePotencyPatternPager;
import cn.edu.sgu.www.mhxysy.service.potency.PotencyFruitPatternService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class PotencyFruitPatternServiceImpl implements PotencyFruitPatternService {

    private final PotencyFruitPatternMapper potencyFruitPatternMapper;

    @Autowired
    public PotencyFruitPatternServiceImpl(PotencyFruitPatternMapper potencyFruitPatternMapper) {
        this.potencyFruitPatternMapper = potencyFruitPatternMapper;
    }

    @Override
    public void insert(PotencyFruitPatternDTO potencyFruitPatternDTO) {
        PotencyFruitPattern potencyFruitPattern = toEntity(potencyFruitPatternDTO);

        potencyFruitPattern.setId(null);

        potencyFruitPatternMapper.insert(potencyFruitPattern);
    }

    @Override
    public void deleteById(Integer id) {
        potencyFruitPatternMapper.deleteById(id);
    }

    @Override
    public void updateById(PotencyFruitPatternDTO potencyFruitPatternDTO) {
        ObjectUtils.requireNonNull(potencyFruitPatternDTO.getId(), CustomMessages.ID_NOT_NULL);

        PotencyFruitPattern potencyFruitPattern = toEntity(potencyFruitPatternDTO);

        potencyFruitPatternMapper.updateById(potencyFruitPattern);
    }

    @Override
    public Page<PotencyFruitPattern> selectByPage(RolePotencyPatternPager pager) {
        QueryWrapper<PotencyFruitPattern> queryWrapper = new QueryWrapper<>();
        Page<PotencyFruitPattern> page = Pager.ofPage(pager);

        queryWrapper.eq(
                StringUtils.isNotEmpty(pager.getRoleId()),
                "role_id", pager.getRoleId()
        );
        queryWrapper.eq(
                pager.getPatternId() != null,
                "pattern_id", pager.getPatternId()
        );

        return potencyFruitPatternMapper.selectPage(page, queryWrapper);
    }

    private PotencyFruitPattern toEntity(PotencyFruitPatternDTO potencyFruitPatternDTO) {
        PotencyFruitPattern potencyFruitPattern = new PotencyFruitPattern();

        potencyFruitPattern.setId(potencyFruitPatternDTO.getId());
        potencyFruitPattern.setRoleId(potencyFruitPatternDTO.getRoleId());
        potencyFruitPattern.setSchemaId(potencyFruitPatternDTO.getSchemaId());
        potencyFruitPattern.setPatternId(potencyFruitPatternDTO.getPatternId());

        return potencyFruitPattern;
    }

}